package com.honghao.student.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.honghao.student.pojo.Student;
import com.honghao.student.service.IStudentService;
@Controller
public class HomeController extends AbstroctController{

	private static final Logger LOGGER = LoggerFactory
			.getLogger(HomeController.class);
	
	@Autowired
	@Qualifier("StudentServiceImpl")
	private IStudentService studentService;

	@Autowired
	@Qualifier("StudentServiceMongoDBImpl")
	private IStudentService stuServiceMongo;

	/**
	 * mysql jsp视图显示
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getstudent", method = RequestMethod.GET)
	public ModelAndView getStudent(HttpServletResponse response)
			throws IOException {
		LOGGER.info("get allStudent ---jsp");
		ModelAndView mav = new ModelAndView();
		List<Student> stulst = studentService.getAllStudent();
		mav.addObject("student", stulst);
		mav.setViewName("home.jsp");
		return mav;
	}
	
	/**
	 * mysql vm视图显示
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getstudentvm", method = RequestMethod.GET)
	public ModelAndView getStudentvm(HttpServletResponse response)
			throws IOException {
		LOGGER.info("get allStudent ---vm");
		ModelAndView mav = new ModelAndView();
		List<Student> stulst = studentService.getAllStudent();
		mav.addObject("student", stulst);
		mav.setViewName("home.vm");
		return mav;
	}

	/**
	 * 解析输入json数据格式
	 * @param stu
	 * @return
	 */
	@RequestMapping("/json1")
	@ResponseBody
	public Student testJson1() {
		LOGGER.info("get json annotation");
		Student stu = new Student();
		stu.setStuId(789);
		stu.setStuName("xiaoqi");
		return stu;
	}
	
	@RequestMapping("/json2")
	public ResponseEntity<Student> testJson2(){
		LOGGER.info("get json ResponseEntity");
		Student stu = new Student();
		stu.setStuId(999);
		stu.setStuName("zhaoba");
		return new ResponseEntity<Student>(stu,HttpStatus.OK);
	}
	
	@SuppressWarnings("unused")
    @RequestMapping("/exceptiontest")
	public String exceptionTest(){
	    if(1 > 0)throw new RuntimeException("test exception");
	    return null;
	}
	
	/**
	 * mongoDB
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/getstudentformongo", method = RequestMethod.GET)
	public ModelAndView getStudentForMongo(HttpServletResponse response)
			throws IOException {
		LOGGER.info("get allStudent for mongo");
		ModelAndView mav = new ModelAndView();
		List<Student> stulst = stuServiceMongo.getAllStudent();
		mav.addObject("student", stulst);
		mav.setViewName("home");
		return mav;
	}

	@RequestMapping(value = "/addstudentformon")
	public String addStudentForMon() {
		LOGGER.info("add student for mongo");
		Student stu = new Student();
		stu.setStuId(456);
		stu.setStuName("lisi");
		stuServiceMongo.register(stu);
		return "redirect:/getstudentformongo";
	}

	// 通过url访问新增页面
	@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
	public String userNew(Model model) {
		return "add";
	}
	
	@RequestMapping(value="/testaddstu")
	public String addStudent(){
	    Student stu = new Student();
	    stu.setStuId(777);
	    stu.setStuName("xiaoqi");
	    stu.setPassword("xiaoqi33");
	    stu.setEmail("xiaoqi@163.com");
	    studentService.register(stu);
	    return "redirect:/getstudent";
	}
	
	//添加新学生信息
	@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
	public String addStudent(@Valid @ModelAttribute("student") Student student,
			BindingResult bindingResult, Model model) {
		if (bindingResult.hasErrors()) {
			model.addAttribute("show", "error");
			//失败,跳转到add.jsp显示
			return "add";
		}
		studentService.register(student);
		//注册成功跳转到列表详细页面,有以下几种方法
		//1.new ModelAndView("redirect:/getstudent");
		return "redirect:/getstudent";
	}
	
	/**
	 * download excel
	 * @param mm
	 * @return
	 */
	@RequestMapping(value="/showStuListByxls")
	public String showStuListExcle(ModelMap mm){
		LOGGER.info("dowload excle");
		List<Student> stulst = studentService.getAllStudent();
		mm.addAttribute("stuList", stulst);
		return "stuListExcel";
	}
	
	/**
	 * del stu.
	 * @param stuId
	 * @return
	 */
	@RequestMapping(value = "/delete", params = "stuId")
	public String delete(@RequestParam("stuId") int stuId) {
		LOGGER.info("del Student");
		return "redirect:/getstudent";
	}
}
